package com.example.administrator.babymusic.ui.activity;


import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;


import com.example.administrator.babymusic.R;
import com.example.administrator.babymusic.ui.fragment.BaseFragment;
import com.example.administrator.babymusic.ui.fragment.EverydayFragment;
import com.example.administrator.babymusic.ui.fragment.MineFragment;
import com.example.administrator.babymusic.ui.fragment.MoreFragment;

/**
 * Created by Administrator on 2016/11/1 0001.
 */

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup rg_tab_bar;
    private RadioButton  rb_channe1, rb_channe2, rb_channe3;

    //Fragment Object
    private BaseFragment fg1,fg2,fg3;
    private FragmentManager fManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        fManager = getSupportFragmentManager();
        rg_tab_bar = (RadioGroup) findViewById(R.id.rg_tab_bar);
        rg_tab_bar.setOnCheckedChangeListener(this);
        //获取各个单选按钮，并设置第二个其为选中状态
        rb_channe1 = (RadioButton) findViewById(R.id.rb_mine);
        rb_channe2 = (RadioButton) findViewById(R.id.rb_everyday);
        rb_channe3 = (RadioButton) findViewById(R.id.rb_more);
        rb_channe2.setTextSize(20);
        rb_channe2.setChecked(true);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction fTransaction = fManager.beginTransaction();
        hideAllFragment(fTransaction);
        switch (checkedId){
            case R.id.rb_mine:
                rb_channe1.setTextSize(23);
                rb_channe2.setTextSize(20);
                rb_channe3.setTextSize(20);
                if(fg1 == null){
                    fg1 = new MineFragment();
                    //fg1.setContent("第一个Fragment");
                    fTransaction.add(R.id.ly_content,fg1, "MineFragment");
                }else{
                    fTransaction.show(fg1);
                }
                break;
            case R.id.rb_everyday:
                rb_channe1.setTextSize(20);
                rb_channe2.setTextSize(23);
                rb_channe3.setTextSize(20);
                if(fg2 == null){
                    fg2 = new EverydayFragment();
                    //fg2.setContent("第二个Fragment");
                    fTransaction.add(R.id.ly_content,fg2, "EverydayFragment");
                }else{
                    fTransaction.show(fg2);
                }
                break;
            case R.id.rb_more:
                rb_channe1.setTextSize(20);
                rb_channe2.setTextSize(20);
                rb_channe3.setTextSize(23);
                if(fg3 == null){
                    fg3 = new MoreFragment();
                    fTransaction.add(R.id.ly_content,fg3, "MoreFragment");
                }else{
                    fTransaction.show(fg3);
                }
                break;
        }
        fTransaction.commit();
    }

    //隐藏所有Fragment
    private void hideAllFragment(FragmentTransaction fragmentTransaction){
        if(fg1 != null)fragmentTransaction.hide(fg1);
        if(fg2 != null)fragmentTransaction.hide(fg2);
        if(fg3 != null)fragmentTransaction.hide(fg3);
    }

}

